<template>
  <div class="register">
    <div class="register-background">
      <div class="grid-overlay"></div>
    </div>

    <div class="register-container">
      <div class="register-left">
        <div class="register-left-content">
          <div class="logo-section">
            <div class="logo-icon">
              <svg-icon icon-class="dashboard" class="logo-svg" />
            </div>
            <h2>智能办公应用管理系统</h2>
            <p>提升办公效率 · 智能协同办公 · 数据驱动决策</p>
          </div>
          <div class="feature-list">
            <div class="feature-item">
              <div class="feature-icon">
                <svg-icon icon-class="chart" />
              </div>
              <span>智能数据分析</span>
            </div>
            <div class="feature-item">
              <div class="feature-icon">
                <svg-icon icon-class="documentation" />
              </div>
              <span>文档智能管理</span>
            </div>
            <div class="feature-item">
              <div class="feature-icon">
                <svg-icon icon-class="peoples" />
              </div>
              <span>团队协作办公</span>
            </div>
          </div>
          <div class="system-status">
            <div class="status-item">
              <div class="status-dot online"></div>
              <span>系统运行正常</span>
            </div>
            <div class="status-item">
              <div class="status-dot secure"></div>
              <span>数据安全加密</span>
            </div>
          </div>
        </div>
      </div>
      <div class="register-right">
        <div class="register-card">
          <div class="card-header">
            <h3 class="title">用户注册</h3>
            <p class="subtitle">创建您的账号，开启智能办公之旅</p>
          </div>
          <el-form
            ref="registerForm"
            :model="registerForm"
            :rules="registerRules"
            class="register-form"
          >
            <el-form-item prop="username">
              <el-input
                v-model="registerForm.username"
                type="text"
                auto-complete="off"
                placeholder="请输入用户名"
                class="modern-input"
              >
                <svg-icon
                  slot="prefix"
                  icon-class="user"
                  class="el-input__icon input-icon"
                />
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                v-model="registerForm.password"
                type="password"
                auto-complete="off"
                placeholder="请输入密码"
                class="modern-input"
              >
                <svg-icon
                  slot="prefix"
                  icon-class="password"
                  class="el-input__icon input-icon"
                />
              </el-input>
            </el-form-item>
            <el-form-item prop="confirmPassword">
              <el-input
                v-model="registerForm.confirmPassword"
                type="password"
                auto-complete="off"
                placeholder="请确认密码"
                class="modern-input"
              >
                <svg-icon
                  slot="prefix"
                  icon-class="password"
                  class="el-input__icon input-icon"
                />
              </el-input>
            </el-form-item>
            <el-form-item prop="email">
              <el-input
                v-model="registerForm.email"
                type="email"
                auto-complete="off"
                placeholder="请输入邮箱"
                class="modern-input"
              >
                <svg-icon
                  slot="prefix"
                  icon-class="email"
                  class="el-input__icon input-icon"
                />
              </el-input>
            </el-form-item>
            <el-form-item prop="code" v-if="captchaOnOff">
              <div class="captcha-container">
                <el-input
                  v-model="registerForm.code"
                  auto-complete="off"
                  placeholder="验证码"
                  class="modern-input captcha-input"
                >
                  <svg-icon
                    slot="prefix"
                    icon-class="validCode"
                    class="el-input__icon input-icon"
                  />
                </el-input>
                <div class="register-code">
                  <img
                    :src="codeUrl"
                    @click="getCode"
                    class="register-code-img"
                  />
                </div>
              </div>
            </el-form-item>
            <el-form-item style="width: 100%">
              <el-button
                size="medium"
                type="primary"
                class="register-button"
                :class="{ 'is-loading': loading }"
                @click.native.prevent="handleRegister"
              >
                <span v-if="!loading">
                  <svg-icon icon-class="edit" class="button-icon" />
                  注 册
                </span>
                <span v-else> 注 册 中... </span>
              </el-button>
              <div class="login-link">
                <span>已有账号？</span>
                <router-link class="link-type" :to="'/login'"
                  >返回登录</router-link
                >
              </div>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <div class="el-register-footer">
      <!-- <p>Copyright © 2025 智能办公应用管理系统 All Rights Reserved</p> -->
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";

export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      codeUrl: "",
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
        email: "",
        code: "",
        uuid: "",
      },
      registerRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入用户名" },
          {
            min: 2,
            max: 20,
            message: "用户名字数必须在2-20个字符之间",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入密码" },
          {
            min: 6,
            max: 20,
            message: "密码长度必须在6-20个字符之间",
            trigger: "blur",
          },
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入密码" },
          { required: true, validator: equalToPassword, trigger: "blur" },
        ],
        email: [
          { required: true, trigger: "blur", message: "请输入邮箱地址" },
          { type: "email", message: "请输入正确的邮箱地址", trigger: "blur" },
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }],
      },
      loading: false,
      captchaOnOff: true,
    };
  },
  created() {
    this.getCode();
  },
  methods: {
    getCode() {
      getCodeImg().then((res) => {
        this.captchaOnOff =
          res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.registerForm.uuid = res.uuid;
        }
      });
    },
    handleRegister() {
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$store
            .dispatch("Register", this.registerForm)
            .then(() => {
              this.$modal.msgSuccess("注册成功，请登录");
              this.$router.push("/login");
            })
            .catch(() => {
              this.loading = false;
              if (this.captchaOnOff) {
                this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: url('~@/assets/images/background3.png') no-repeat center center;
  background-size: cover;
  position: relative;
  overflow: hidden;
}

.register-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}



.grid-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: linear-gradient(
      rgba(255, 255, 255, 0.1) 1px,
      transparent 1px
    ),
    linear-gradient(90deg, rgba(255, 255, 255, 0.1) 1px, transparent 1px);
  background-size: 50px 50px;
  animation: grid-move 20s linear infinite;
  opacity: 0.6;
}



@keyframes grid-move {
  0% {
    transform: translate(0, 0);
  }
  100% {
    transform: translate(50px, 50px);
  }
}

.register-container {
  display: flex;
  width: 1200px;
  height: 800px;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 20px;
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  position: relative;
  z-index: 1;
  backdrop-filter: blur(15px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  animation: slideInUp 0.8s ease-out;
}

.register-left {
  flex: 1.2;
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px 40px;
  color: #2d3748;
  position: relative;
  overflow: hidden;

  &::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="25" cy="25" r="1" fill="rgba(102,126,234,0.1)"/><circle cx="75" cy="75" r="1" fill="rgba(102,126,234,0.1)"/><circle cx="50" cy="10" r="0.5" fill="rgba(102,126,234,0.1)"/><circle cx="10" cy="60" r="0.5" fill="rgba(102,126,234,0.1)"/><circle cx="90" cy="40" r="0.5" fill="rgba(102,126,234,0.1)"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
    opacity: 0.3;
  }
}

.register-left-content {
  text-align: center;
  z-index: 1;
  width: 100%;
}

.logo-section {
  margin-bottom: 40px;

  .logo-icon {
    width: 80px;
    height: 80px;
    background: rgba(102, 126, 234, 0.1);
    border-radius: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 20px;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(102, 126, 234, 0.2);
    animation: logo-pulse 2s ease-in-out infinite;

    .logo-svg {
      font-size: 40px;
      color: #667eea;
    }
  }

  h2 {
    font-size: 2.2em;
    margin-bottom: 15px;
    font-weight: 600;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    color: #2d3748;
  }

  p {
    font-size: 1em;
    opacity: 0.8;
    line-height: 1.6;
    color: #4a5568;
  }
}

.feature-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
  margin-top: 40px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 15px 20px;
  background: rgba(255, 255, 255, 0.6);
  border-radius: 12px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(102, 126, 234, 0.1);
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);

  &:hover {
    transform: translateX(10px);
    background: rgba(255, 255, 255, 0.8);
    border-color: rgba(102, 126, 234, 0.2);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  }

  .feature-icon {
    width: 40px;
    height: 40px;
    background: rgba(102, 126, 234, 0.1);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;

    .svg-icon {
      font-size: 20px;
      color: #667eea;
    }
  }

  span {
    font-size: 16px;
    font-weight: 500;
    color: #2d3748;
  }
}

@keyframes logo-pulse {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
}

.system-status {
  margin-top: 30px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.status-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 15px;
  background: rgba(255, 255, 255, 0.6);
  border-radius: 8px;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(102, 126, 234, 0.1);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);

  .status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;

    &.online {
      background: #48bb78;
      animation: pulse 2s infinite;
    }

    &.secure {
      background: #4299e1;
      animation: pulse 2s infinite 1s;
    }
  }

  span {
    font-size: 14px;
    font-weight: 500;
    color: #2d3748;
  }
}

@keyframes pulse {
  0%,
  100% {
    opacity: 1;
    transform: scale(1);
  }
  50% {
    opacity: 0.7;
    transform: scale(1.2);
  }
}

.register-right {
  flex: 1;
  padding: 40px;
  display: flex;
  align-items: center;
  background: #f8fafc;
}

.register-card {
  width: 100%;
  background: white;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  padding: 40px;
  border: 1px solid rgba(0, 0, 0, 0.05);
  animation: fadeInScale 0.6s ease-out 0.3s both;
}

.card-header {
  text-align: center;
  margin-bottom: 30px;

  .title {
    font-size: 28px;
    color: #1a202c;
    font-weight: 700;
    margin-bottom: 8px;
  }

  .subtitle {
    font-size: 14px;
    color: #718096;
  }
}

.register-form {
  .modern-input {
    .el-input__inner {
      height: 50px;
      padding-left: 50px;
      border-radius: 12px;
      border: 2px solid #e2e8f0;
      transition: all 0.3s ease;
      font-size: 16px;
      background: #f7fafc;
      position: relative;
      overflow: hidden;

      &:focus {
        border-color: #667eea;
        box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        background: white;
        transform: translateY(-1px);
      }

      &::before {
        content: "";
        position: absolute;
        top: 0;
        left: -100%;
        width: 100%;
        height: 100%;
        background: linear-gradient(
          90deg,
          transparent,
          rgba(102, 126, 234, 0.1),
          transparent
        );
        transition: left 0.5s;
      }

      &:focus::before {
        left: 100%;
      }
    }
  }

  .input-icon {
    height: 50px;
    width: 20px;
    margin-left: 15px;
    color: #a0aec0;
    font-size: 18px;
    transition: all 0.3s ease;
  }

  .modern-input:focus-within .input-icon {
    color: #667eea;
    transform: scale(1.1);
  }
}

.captcha-container {
  display: flex;
  gap: 15px;
  align-items: center;

  .captcha-input {
    flex: 1;
  }
}

.register-code {
  width: 120px;
  height: 50px;

  img {
    height: 50px;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.3s ease;
    border: 2px solid #e2e8f0;

    &:hover {
      transform: scale(1.02);
      border-color: #667eea;
    }
  }
}

.register-button {
  width: 100%;
  height: 50px;
  border-radius: 12px;
  font-size: 16px;
  font-weight: 600;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  position: relative;
  overflow: hidden;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 25px rgba(102, 126, 234, 0.3);
  }

  &:active {
    transform: translateY(0);
  }

  &::before {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 0;
    height: 0;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.3);
    transform: translate(-50%, -50%);
    transition: width 0.6s, height 0.6s;
  }

  &:active::before {
    width: 300px;
    height: 300px;
  }

  .button-icon {
    font-size: 18px;
  }

  &.is-loading {
    background: linear-gradient(135deg, #a0aec0 0%, #718096 100%);
    cursor: not-allowed;

    &::after {
      content: "";
      position: absolute;
      width: 20px;
      height: 20px;
      border: 2px solid transparent;
      border-top: 2px solid white;
      border-radius: 50%;
      animation: spin 1s linear infinite;
    }
  }
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

.login-link {
  text-align: center;
  margin-top: 20px;

  span {
    color: #718096;
    font-size: 14px;
  }

  .link-type {
    color: #667eea;
    text-decoration: none;
    font-size: 14px;
    font-weight: 600;
    margin-left: 5px;
    transition: all 0.3s ease;

    &:hover {
      color: #764ba2;
      text-decoration: underline;
    }
  }
}

.el-register-footer {
  position: fixed;
  bottom: 20px;
  width: 100%;
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  z-index: 1;
}

// 响应式设计
@media (max-width: 1200px) {
  .register-container {
    width: 90%;
    max-width: 1000px;
  }
}

@media (max-width: 768px) {
  .register-container {
    flex-direction: column;
    height: auto;
    min-height: 100vh;
  }

  .register-left {
    padding: 40px 20px;
  }

  .register-right {
    padding: 20px;
  }

  .feature-list {
    flex-direction: row;
    flex-wrap: wrap;
    gap: 10px;
  }

  .feature-item {
    flex: 1;
    min-width: 200px;
  }
}

@keyframes slideInUp {
  from {
    opacity: 0;
    transform: translateY(50px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes fadeInScale {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}
</style>

